from tortoise import fields
from model.CommonModel import CommonModel
from datetime import date


class Clas(CommonModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255,description="班级名称")
    addr = fields.CharField(max_length=255,description="班级地址",null=True)

    # 关系
    students = fields.ReverseRelation["Student"]


class Course(CommonModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255,description="课程名称")
    teacher = fields.ForeignKeyField("models.Teacher",related_name="courses",description="教师")

class Teacher(CommonModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, description="姓名")
    tno = fields.CharField(max_length=255, description="工号")

class Student(CommonModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255,description="姓名")
    age = fields.IntField(description="年龄")
    birth = fields.DateField(description="生日",default=date.today(),null=True)
    sno = fields.CharField(max_length=255,description="学号")

    class_id = fields.ForeignKeyField("models.Clas",related_name="students",description="班级")
    courses = fields.ManyToManyField("models.Course",related_name="students",description="课程")

    # 使用 Meta 类定义模型的额外设置
    class Meta:
        table = "student"                   # 表名
        table_description = "学生表"        # 描述
        indexes = ["sno"]                   # 索引